//package com.steamedfish.auctionsecurityserveroauth2.filter;
//
//import com.auth0.jwt.JWT;
//import com.auth0.jwt.algorithms.Algorithm;
//import org.codehaus.jackson.map.ObjectMapper;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.config.annotation.rsocket.RSocketSecurity;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.userdetails.User;
//
//import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
//import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
//import org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter;
//import org.springframework.security.web.util.matcher.RequestMatcher;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.io.PrintWriter;
//import java.security.Principal;
//import java.util.Collection;
//import java.util.Date;
//import java.util.HashMap;
//import java.util.Map;
//
//
///**
// * @author meetml
// */
//@Component
//public class TokenAuthenticationFilter1 extends AbstractAuthenticationProcessingFilter {
//
//    private static final String SECRET = "shanfushanfu";
//
//    //过期时间1分钟
//    private static final int EXPIRES_TIME = 60000;
//
//    protected TokenAuthenticationFilter1(String defaultFilterProcessesUrl) {
//        super(defaultFilterProcessesUrl);
//    }
//
//
//    @Override
//    protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
//
//
//
//        //获取登陆角色
//        Collection<? extends GrantedAuthority> authorities = authResult.getAuthorities();
//        StringBuffer authoritiesBuffer = new StringBuffer();
//
//        //遍历角存储到buffer中
//        for (GrantedAuthority authority : authorities) {
//            authoritiesBuffer.append(authority.getAuthority()).append(",");
//        }
//
//        //获取用户信息
////        authResult.getPrincipal()
//
//
//        //定义token头部
//        Map<String,Object> map = new HashMap<String,Object>();
//        map.put("alg", "HS256");
//        map.put("typ", "JWT");
//
//        //创建签发时间
//        Date nowDate = new Date();
//        //创建过期时间
//        Date expiresDate = new Date(System.currentTimeMillis()+EXPIRES_TIME);
//
//        String jwt = JWT.create()
//                .withHeader(map)
//                .withClaim("authorities", authoritiesBuffer.toString())
////                .withClaim("principal",)
//                .withClaim("username", authResult.getName())
//                .withIssuedAt(nowDate)
//                .withExpiresAt(expiresDate)
////                .withSubject()
//                .sign(Algorithm.HMAC256(SECRET));
//
//
//        Map<String, String> resMap = new HashMap<>();
//        resMap.put("token", jwt);
//        resMap.put("msg", "Success");
//
//        response.setContentType("application/json;chartset=utf-8");
//
//        PrintWriter out = response.getWriter();
//        out.write(new ObjectMapper().writeValueAsString(map));
//        out.flush();
//        out.close();
//        super.successfulAuthentication(request, response, chain, authResult);
//    }
//
//    @Override
//    protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException, ServletException {
//        super.unsuccessfulAuthentication(request, response, failed);
//    }
//
//    @Override
//    public Authentication attemptAuthentication(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws AuthenticationException, IOException, ServletException {
//        //获取到用户信息
//        User user = new ObjectMapper().readValue(httpServletRequest.getInputStream(), User.class);
//        //开始验证
//        return getAuthenticationManager().authenticate(new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword()));
//    }
//
//
//}